<?php

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\model\Pay;
use app\common\Frontend;
use app\common\model\Article;
use app\common\model\ArticleType;
use app\common\model\Banner;
use app\common\model\HotSearch;
use app\common\model\Merchant;
use app\common\model\Navigation;
use app\common\model\PublicStatus;
use app\common\model\User;
use general\Tree;
use Qiniu\Auth;
use think\facade\Config;
use think\Request;

class Home extends Frontend
{

    protected array $noNeedLogin = ['isLogin','webConfigSite'];
//    protected array $noNeedLogin = ['getBannerList', 'getArticleList', 'getArticleTypeList', 'getArticleTypeList', 'getNavigaion','getNavById', 'hotSearch', 'getArticleDetail','isLogin','webConfigSite'];
    public function initialize(): void
    {
        parent::initialize();
    }
    //首页轮播图
    public function getBannerList()
    {
        $where = [];
        if ($this->request->has('type') && !empty($this->request->param('type'))) {
            $where[] = ['type', '=', $this->request->param('type')];
        }
        $res = Banner::where($where)->order('weigh desc')->select()->each(function ($item, $key) {

            $item['full_url'] =  full_url($item['img']);
            return $item;
        });
        $this->success('', [
            'list'   => $res->toArray(),
            'remark' => '',
        ]);
    }
    //首页公告
    public function getArticleList(): void
    {
        $limit = $this->request->param('limit', 10);
        $where = [];
        if ($this->request->has('type_id') && !empty($this->request->param('type_id'))) {
            $where[] = ['article_type_id', '=', $this->request->param('type_id')];
        }
        $res = Article::where($where)->paginate($limit)->each(function ($item, $key) {

            $item['full_url'] =  full_url($item['url']);
            return $item;
        });;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ]);
    }

    //首页公告详情
    public function getArticleDetail(): void
    {
        $id  = $this->request->param('id');

        $row = Article::find($id);
        if (!$row) {
            $this->error('未找到文章');
        }
        $this->success('', [
            'list'   => $row,
            'remark' => '',
        ]);
    }

    //文章类型
    public function getArticleTypeList(): void
    {
        $res = ArticleType::select()->toArray();
        $this->success('', [
            'list'   => $res,
            'remark' => '',
        ]);
    }
    //金刚区
    public function getNavigaion(): void
    {
        $where = [];
        if ($this->request->has('type') && !empty($this->request->param('type'))) {
            $where[] = ['type', '=', $this->request->param('type')];
        }
        $where[] = ['status', '=', PublicStatus::PUBLIC_STATUS_ONE];
        $res = Navigation::where($where)->order('weigh desc')->select()->toArray();
        $data = Tree::instance()->assembleChild($res);
        //后面存储文件类型
        $this->success('', [
            'list' => [
                'all'=>$data,
            ],
            'remark' => '',
        ]);
    }


    public function getNavById(){
        $where = [];
        $where[] = ['status', '=', PublicStatus::PUBLIC_STATUS_ONE];
        $res = Navigation::where($where)->select()->toArray();
        $tree = Tree::instance()->init($res);
        if ($this->request->has('nav_id') && !empty($this->request->param('nav_id'))) {
            $childIds = $tree->getChildrenIds($this->request->param('nav_id'), true);
            $where[] = ['id', 'in', $childIds];
        }
        $res = Navigation::where($where)->order('weigh desc')->select()->toArray();
        $data = Tree::instance()->assembleChild($res);
        //后面存储文件类型
        $this->success('', [
            'list' => $data,
            'remark' => '',
        ]);
    }


    //热门搜索
    public function hotSearch(): void
    {
        $res = HotSearch::where(['status' => PublicStatus::PUBLIC_STATUS_ONE])->order('weigh desc')->select()->toArray();
        $this->success('', [
            'list'   => $res,
            'remark' => '',
        ]);
    }


    //是否登录
    public function isLogin(): void
    {
        $is_flag = $this->auth->isLogin();
        $this->success('', [
            'list' => $is_flag
        ]);
    }

    //配置文件
    public function webConfigSite() : void {
        $site_config = [
            'site_name'=>get_sys_config('site_name'),
            'record_number'=>get_sys_config('record_number'),
            'wx_img'=>(get_sys_config('wx_img')),
            'sm'=>get_sys_config('sm'),
            'logo'=>(get_sys_config('logo')),
            'logo2'=>(get_sys_config('logo2')),
            'logo3'=>(get_sys_config('logo3')),
            'login_img'=>(get_sys_config('login_img')),
            'default_head'=>(get_sys_config('default_head')),
            'defalut_product_img'=>(get_sys_config('defalut_product_img')),
            'default_video_img'=>(get_sys_config('video_default_url')),
            'progress_download'=>(get_sys_config('progress_download')),
            'accomplish_download'=>(get_sys_config('accomplish_download')),
            'download_pay_img'=>(get_sys_config('download_pay_img')),
            'download_day'=>(get_sys_config('download_day')),
            'recharge'=>(get_sys_config('','recharge')),
            'ad'=>(get_sys_config('','ad')),
            'subscribe'=>(get_sys_config('','subscribe')),
            'agreement'=>(get_sys_config('','agreement')),
            'login_page'=>(get_sys_config('','login')),
            'is_download'=>(get_sys_config('is_download')),
        ];
        $this->success('', [
            'list' => $site_config
        ]);
    }
}
